package algorithm.array;

import java.util.*;

/**
 * 给你一个字符串数组 words ，只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
 */
public class FindWords {

    public static void main(String[] args) {
        String [] words = {"Hello","Alaska","Dad","Peace"};
        findWords(words);
        System.out.println(Arrays.toString(findWords(words)));
    }
    public static String[] findWords(String[] words) {
        String [] line = {"qwertyuiop", "asdfghjkl","zxcvbnm"};
        Map<Character, Integer> map = new HashMap();
        List<String> list = new ArrayList<>();

        for (int i = 0; i < line.length; i++) {
            for (int j = 0; j < line[i].length(); j++) {
                map.put(line[i].charAt(j), i+1);
            }
        }

        for (int i = 0; i < words.length; i++) {
            boolean isValid = true;
            Integer integer = map.get(words[i].toLowerCase().charAt(0));
            int j = 1;
            while (j < words[i].length()){
                if (map.get(words[i].toLowerCase().charAt(j)) != integer){
                    isValid = false;
                    break;
                }
                j++;
            }
            if (isValid){
                list.add(words[i]);
            }
        }

        String[] s = new String[list.size()];

        for (int i = 0; i < list.size(); i++) {
            s[i] = list.get(i);
        }
        return s;
    }
}
